﻿var webSocket = {};
var webSocket_PE = {};

var islianjie = false;
var islianjie_PE = false;
var isclose = false;//是否主动关闭 关闭之后不在自动连接
var isclose_PE = false;
//创建websockt
function CreateWebSocket() {
    isclose = false;
    webSocket = new WebSocket("wss://www.gps808.cn:8142/wss");
    webSocket.onopen = WebSokectOnOpen;
    webSocket.onmessage = WebSocketOnMessage;
    webSocket.onclose = WebSocketOnClose;
};

//建立连接事件
function WebSokectOnOpen() {
    islianjie = true;
};
function WebSocketis_close()
{
    isclose = true;
    islianjie = false;
    webSocket.close();
}


//监听事件
function WebSocketOnMessage(event) {
    //监听来自服务端的数据
    InstructionProcessing(event.data);
};



function WebSocketOnClose() {
    if (!isclose) {
        //监听来自客户端的数据
        //alert('和服务器断开连接');
        islianjie = false;
        setTimeout(function () {
            webSocket = new WebSocket("wss://www.gps808.cn:8142/wss");
            webSocket.onopen = WebSokectOnOpen;
            webSocket.onmessage = WebSocketOnMessage;
            webSocket.onclose = WebSocketOnClose;
        }, 3000);
        
    }
};
//发送事件
function WebSocketSendMsg(type, sendmsg,isshow) {
    if (islianjie) {
        //获取text中的值
        var text = "0|yztest|xck123456789xck|" + localStorage.getItem("EquipmentNoSet") + "|" + type + "|" + sendmsg;
        //发送到服务器
        webSocket.send(text);
       return true;
    } 
};

//-------------------音频链接推送--------------------
//创建websockt
function CreateWebSocket_PE() {
    isclose_PE = false;
    webSocket_PE = new WebSocket("wss://www.gps808.cn:8142/Ywss");
     webSocket_PE.onopen = WebSokectOnOpen_PE;
     webSocket_PE.onmessage = WebSocketOnMessage_PE;
     webSocket_PE.onclose = WebSocketOnClose_PE;
};

//建立连接事件
function WebSokectOnOpen_PE() {
    islianjie_PE = true;
};
function WebSocketis_close_PE()
{
    isclose_PE = true;
    islianjie_PE = false;
    webSocket_PE.close();
}
//监听事件
function WebSocketOnMessage_PE(event) {
    //监听来自服务端的数据
    InstructionProcessing_PE(event.data);
};

//发送事件
function WebSocketSendMsg_PE(sendmsg) {
    if (islianjie_PE) {
        //发送到服务器
        webSocket_PE.send(sendmsg);
    } 
};
function WebSocketOnClose_PE() {
    if (!isclose_PE) {
        //监听来自客户端的数据
        islianjie_PE = false;
        setTimeout(function () {
            webSocket_PE = new WebSocket("wss://www.gps808.cn:8142/Ywss");
            webSocket_PE.onopen = WebSokectOnOpen_PE;
            webSocket_PE.onmessage = WebSocketOnMessage_PE;
            webSocket_PE.onclose = WebSocketOnClose_PE;
        }, 3000);
        
    }
};
export {
  CreateWebSocket,
  WebSocketSendMsg,
  WebSocketis_close
}
